package com.zjdiepu.www.manage.api.icbc;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

import java.util.Arrays;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.zjdiepu.www.base.BaseController;
import com.zjdiepu.www.base.annotation.Add;
import com.zjdiepu.www.base.annotation.Update;
import com.zjdiepu.www.base.core.AbstractSysUser;
import com.zjdiepu.www.base.core.ApiResponse;
import com.zjdiepu.www.base.core.PageVo;
import com.zjdiepu.www.base.enums.status.DpStatus;
import com.zjdiepu.www.base.enums.type.DataDicType;
import com.zjdiepu.www.base.enums.type.OperateType;
import com.zjdiepu.www.base.enums.type.ResultFlagType;
import com.zjdiepu.www.common.util.CollectionInitUtil;
import com.zjdiepu.www.ibiz.cache.ICachedDictionaryService;
import com.zjdiepu.www.ibiz.icbc.IYwOpenCardHzicbcService;
import com.zjdiepu.www.ibiz.icbc.IYwOpenCardHzicbcSubService;
import com.zjdiepu.www.model.icbc.dto.YwOpenCardHzicbc;
import com.zjdiepu.www.model.icbc.dto.YwOpenCardHzicbcSub;
import com.zjdiepu.www.model.icbc.ext.YwOpenCardHzicbcSubExt;

/**
 * yw_开卡信息_杭州工行_子表Controller层<br/>
 * @author xiongxiaotun<br/>
 * @time 2017年12月13日 18时05分<br/>
 * @since 1.0<br/>
 */
@RestController
@RequestMapping(value= "YwOpenCardHzicbcSubController", produces="application/json;charset=UTF-8")
@Api(tags = {"yw_开卡信息_杭州工行_子表"}, produces="application/json;charset=UTF-8")
public class YwOpenCardHzicbcSubController extends BaseController {
    @Resource
    private IYwOpenCardHzicbcSubService ywOpenCardHzicbcSubService;
    @Resource
    private IYwOpenCardHzicbcService ywOpenCardHzicbcService;
    @Resource
    private ICachedDictionaryService cachedDictionaryService;

    @ApiOperation(value = "查询yw_开卡信息_杭州工行_子表", response = Boolean.class, httpMethod="POST", tags={"查询"})
    @RequestMapping(value = "selectByFined", method = RequestMethod.POST)
    public ApiResponse selectByFined(@ModelAttribute YwOpenCardHzicbcSub ywOpenCardHzicbcSub){
    	YwOpenCardHzicbc ywOpenCardHzicbc=new YwOpenCardHzicbc();
    	ywOpenCardHzicbc.setBusiId(ywOpenCardHzicbcSub.getBusiId());
    	ywOpenCardHzicbc=ywOpenCardHzicbcService.selectRefined(ywOpenCardHzicbc);
    	ywOpenCardHzicbcSub=ywOpenCardHzicbcSubService.selectRefined(ywOpenCardHzicbcSub);
    	
    	if(ywOpenCardHzicbcSub!=null){
    		if(ywOpenCardHzicbc!=null){
    			ywOpenCardHzicbcSub.setOpenCardStatus(ywOpenCardHzicbc.getOpenCardStatus());
        	}
    		return ApiResponse.success(ywOpenCardHzicbcSub);
    	}
    	return null;
    }
    /**
     * @author xiongxiaotun <br/>
     * @Title: save  <br/>
     * @Description: 新增yw_开卡信息_杭州工行_子表 <br/>
     * @param ywOpenCardHzicbcSub
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "新增yw_开卡信息_杭州工行_子表", response = Boolean.class, httpMethod="POST", tags={"新增"})
    @RequestMapping(value = "save", method = RequestMethod.POST)
    public ApiResponse save(@Validated(Add.class) @ModelAttribute YwOpenCardHzicbcSub ywOpenCardHzicbcSub) {
    	AbstractSysUser currentUser = getCurrentUser();
    	ywOpenCardHzicbcSub.setModifier(currentUser.getId()); //统一使用修改人和修改人名称来初始化
    	ywOpenCardHzicbcSub.setModifierName(currentUser.getName());
        ywOpenCardHzicbcSubService.saveUpdateAndDelete(ywOpenCardHzicbcSub, OperateType.add);
        return ApiResponse.success(true);
    }

    /**
     * @author xiongxiaotun <br/>
     * @Title: remove  <br/>
     * @Description: 删除yw_开卡信息_杭州工行_子表<br/>
     * @param id
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "删除yw_开卡信息_杭州工行_子表", response = Boolean.class, httpMethod="POST", tags={"删除"})
    @ApiImplicitParams({
        @ApiImplicitParam(name = "id", value = "数据主键", paramType="query", required = true, example="1")
    })
    @RequestMapping(value = "remove", method = RequestMethod.POST)
    public ApiResponse remove(String id) {
    	if(StringUtils.isBlank(id)) {
    		return ApiResponse.fail(ResultFlagType.PARAM_EMPTY);
    	}
    	AbstractSysUser currentUser = getCurrentUser();
    	List<String> ids = Arrays.asList(id.split(","));
    	ywOpenCardHzicbcSubService.updateBatch(currentUser.getId(), currentUser.getName(), DpStatus.DELETED.getCode(), ids);
        return ApiResponse.success(true);
    }
    
    /**
     * @author xiongxiaotun <br/>
     * @Title: disable  <br/>
     * @Description: 禁用yw_开卡信息_杭州工行_子表<br/>
     * @param id
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "禁用yw_开卡信息_杭州工行_子表", response = Boolean.class, httpMethod="POST", tags={"禁用"})
    @ApiImplicitParams({
        @ApiImplicitParam(name = "id", value = "数据主键", paramType="query", required = true, example="1")
    })
    @RequestMapping(value = "disable", method = RequestMethod.POST)
    public ApiResponse disable(String id) {
    	if(StringUtils.isBlank(id)) {
    		return ApiResponse.fail(ResultFlagType.PARAM_EMPTY);
    	}
    	AbstractSysUser currentUser = getCurrentUser();
    	List<String> ids = Arrays.asList(id.split(","));
    	ywOpenCardHzicbcSubService.updateBatch(currentUser.getId(), currentUser.getName(), DpStatus.INVALID.getCode(), ids);
        return ApiResponse.success(true);
    }
    
    /**
     * @author xiongxiaotun <br/>
     * @Title: enable <br/>
     * @Description: 启用yw_开卡信息_杭州工行_子表<br/>
     * @param id
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "启用yw_开卡信息_杭州工行_子表", response = Boolean.class, httpMethod="POST", tags={"启用"})
    @ApiImplicitParams({
        @ApiImplicitParam(name = "id", value = "数据主键", paramType="query", required = true, example="1")
    })
    @RequestMapping(value = "enable", method = RequestMethod.POST)
    public ApiResponse enable(String id) {
    	if(StringUtils.isBlank(id)) {
    		return ApiResponse.fail(ResultFlagType.PARAM_EMPTY);
    	}
    	AbstractSysUser currentUser = getCurrentUser();
    	List<String> ids = Arrays.asList(id.split(","));
    	ywOpenCardHzicbcSubService.updateBatch(currentUser.getId(), currentUser.getName(), DpStatus.NORMAL.getCode(), ids);
        return ApiResponse.success(true);
    }

    /**
     * @author xiongxiaotun <br/>
     * @Title: modify  <br/>
     * @Description: 修改yw_开卡信息_杭州工行_子表 <br/>
     * @param ywOpenCardHzicbcSub
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "修改yw_开卡信息_杭州工行_子表", response = Boolean.class, httpMethod="POST", tags={"修改"})
    @RequestMapping(value = "modify", method = RequestMethod.POST)
    public ApiResponse modify(@Validated(Update.class) @ModelAttribute YwOpenCardHzicbcSub ywOpenCardHzicbcSub) {
    	AbstractSysUser currentUser = getCurrentUser();
    	ywOpenCardHzicbcSub.setModifier(currentUser.getId()); //统一使用修改人和修改人名称来初始化
    	ywOpenCardHzicbcSub.setModifierName(currentUser.getName());
    	ywOpenCardHzicbcSubService.saveUpdateAndDelete(ywOpenCardHzicbcSub, OperateType.edit);
        return ApiResponse.success(true);
    }

    /**
     * @author xiongxiaotun <br/>
     * @Title: getById  <br/>
     * @Description: 主键查询yw_开卡信息_杭州工行_子表<br/>
     * @param id
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "主键查询yw_开卡信息_杭州工行_子表", response = YwOpenCardHzicbcSub.class, httpMethod="POST", tags={"主键查询"})
    @ApiImplicitParams({
        @ApiImplicitParam(name = "id", value = "数据主键", paramType="query", required = true, example="1")
    })
    @RequestMapping(value = "getById", method = RequestMethod.POST)
    public ApiResponse getById(String id){
    	if(StringUtils.isBlank(id)) {
    		return ApiResponse.fail(ResultFlagType.PARAM_EMPTY);
    	}
       YwOpenCardHzicbcSub ywOpenCardHzicbcSub = ywOpenCardHzicbcSubService.selectById(id);
       if(null == ywOpenCardHzicbcSub) {
    	   return ApiResponse.fail(ResultFlagType.MISSING);
       }
       return ApiResponse.success(ywOpenCardHzicbcSub);
    }
    
    /**
     * @author xiongxiaotun <br/>
     * @Title: list  <br/>
     * @Description: 条件查询yw_开卡信息_杭州工行_子表 <br/>
     * @param ywOpenCardHzicbcSubExt
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "条件查询yw_开卡信息_杭州工行_子表",response = YwOpenCardHzicbcSub.class, httpMethod="POST", tags={"列表"})
    @RequestMapping(value = "list", method = RequestMethod.POST)
    public ApiResponse list(@ModelAttribute YwOpenCardHzicbcSubExt ywOpenCardHzicbcSubExt){
        ywOpenCardHzicbcSubExt.notDeleted();
        PageVo page = getPage(ywOpenCardHzicbcSubExt);
        int records = ywOpenCardHzicbcSubService.countByExt(ywOpenCardHzicbcSubExt);
        page.setRecords(records);
        if (records > 0) {
        	YwOpenCardHzicbc ywOpenCardHzicbc=null;
        	ywOpenCardHzicbcSubExt.setOrderSql(" created desc");
            List<YwOpenCardHzicbcSub> ywOpenCardHzicbcSubs = ywOpenCardHzicbcSubService.selectByExt(ywOpenCardHzicbcSubExt);
            for (YwOpenCardHzicbcSub ywOpenCardHzicbcSub : ywOpenCardHzicbcSubs) {
            	ywOpenCardHzicbc=new YwOpenCardHzicbc();
            	ywOpenCardHzicbc.setBusiId(ywOpenCardHzicbcSub.getBusiId());
            	ywOpenCardHzicbc=ywOpenCardHzicbcService.selectRefined(ywOpenCardHzicbc);
            	if(ywOpenCardHzicbc!=null){
            		ywOpenCardHzicbcSub.setOpenCardStatus(ywOpenCardHzicbc.getOpenCardStatus());
            		ywOpenCardHzicbcSub.setDrawmodeDisplay(cachedDictionaryService.getName(String.valueOf(ywOpenCardHzicbcSub.getDrawmode()), DataDicType.HZICBC_DRAW_MODE));
    	    		ywOpenCardHzicbcSub.setAccgetmDisplay(cachedDictionaryService.getName(String.valueOf(ywOpenCardHzicbcSub.getAccgetm()), DataDicType.HZICBC_ACC_GETM));
    	    		ywOpenCardHzicbcSub.setEmladdrfDisplay(cachedDictionaryService.getName(String.valueOf(ywOpenCardHzicbcSub.getEmladdrf()), DataDicType.HZICBC_EML_ADDRF));
    	    		ywOpenCardHzicbcSub.setMachgfDisplay(cachedDictionaryService.getName(String.valueOf(ywOpenCardHzicbcSub.getMachgf()), DataDicType.HZICBC_MACHGF));
    	    		ywOpenCardHzicbcSub.setMrtlstatDisplay(cachedDictionaryService.getName(String.valueOf(ywOpenCardHzicbcSub.getMrtlstat()), DataDicType.HZICBC_MRTL_STAT));
    	    		ywOpenCardHzicbcSub.setEdulvlDisplay(cachedDictionaryService.getName(String.valueOf(ywOpenCardHzicbcSub.getEdulvl()), DataDicType.HZICBC_EDU_LVL));
    	    		ywOpenCardHzicbcSub.setHomestatDisplay(cachedDictionaryService.getName(String.valueOf(ywOpenCardHzicbcSub.getHomestat()), DataDicType.HZICBC_HOME_STAT));
    	    		ywOpenCardHzicbcSub.setModelcodeDisplay(cachedDictionaryService.getName(String.valueOf(ywOpenCardHzicbcSub.getModelcode()), DataDicType.HZICBC_MODEL_CODE));
    	    		if(ywOpenCardHzicbcSub.getReltsex1()!=null){
    	    			ywOpenCardHzicbcSub.setReltsex1Display(ywOpenCardHzicbcSub.getReltsex1().equals("1")?"男":"女");
    	    		}
    	    		if(ywOpenCardHzicbcSub.getReltsex2()!=null){
    	    			ywOpenCardHzicbcSub.setReltsex2Display(ywOpenCardHzicbcSub.getReltsex2().equals("1")?"男":"女");
    	    		}
    	    		ywOpenCardHzicbcSub.setOccptnDisplay(cachedDictionaryService.getName(String.valueOf(ywOpenCardHzicbcSub.getOccptn()), DataDicType.JOB));
    	    		ywOpenCardHzicbcSub.setReltship1Display(cachedDictionaryService.getName(String.valueOf(ywOpenCardHzicbcSub.getReltship1()), DataDicType.CONTACT_RELATION));
    	    		ywOpenCardHzicbcSub.setReltship2Display(cachedDictionaryService.getName(String.valueOf(ywOpenCardHzicbcSub.getReltship2()), DataDicType.CONTACT_RELATION));
            	}
			}
            page.setRows(ywOpenCardHzicbcSubs);
        } else {
            page.setRows(CollectionInitUtil.emptyList());
        }
        return ApiResponse.success(page);
    }

}
